<?php

namespace app\api\controller;



use addons\epay\library\Service;
use app\api\model\IncubatorOrder;
use app\common\controller\Api;
use think\Exception;

/**
 * 保温箱订单
 */
class Incubator extends Api
{
    protected $noNeedLogin = ['incubatornotifyx'];
    protected $noNeedRight = ['*'];

    //创建保温箱并支付
    public function create()
    {
        $num = input('num',1);
        if ($num<=0)$this->error('保温箱数量不可为0');
        try {

            $card_model = new IncubatorOrder();

            $res = $card_model->add($this->auth->id,$num);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);

    }

    public function pay()
    {
        $id = input('id',0);
        $pay_type = input('pay_type','');
        $order = IncubatorOrder::get($id);

        if ($pay_type=='wechat'){

            $request = request();
            $notifyurl =  $request->root(true)."/api/incubator/incubatornotifyx";

            $pay = \addons\epay\library\Service::submitOrder($order['price'],$order['order_no'],'wechat','保温箱押金',$notifyurl,'','miniapp',$this->auth->wechat_open_id);

            $this->success("请求成功",$pay);
        } else {
            if ($this->auth->money<$order['price']){
                $this->error('余额不足');
            }
            \app\common\model\User::money(-$order['price'],$this->auth->id,'保温箱押金付款');
            $order->pay_type = 'balance';
            $order->pay_status = 1;
            $order->pay_time = time();
            $res = $order->save();
            $data = [
                'is_incubator'=>1,
                'incubator_deposit'=>$order['price'],
                'incubator_order_id'=>$order['id'],
                'incubator_num'=>$order['num'],
                'can_print'=>1
            ];
            \app\common\model\User::update($data,['id'=>$order['user_id']]);
            $this->success("请求成功",$res);
        }

    }


    //解冻
    public function unfreeze()
    {
        try {
            $refund_type = input('refund_type',0);

            $card_model = new IncubatorOrder();

            $res = $card_model->unfreeze($this->auth->id,$refund_type);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //回调
    public function incubatornotifyx()
    {
        $paytype = "wechat";
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        \think\Log::record($data);

        try {
            $out_trade_no = $data['out_trade_no'];
            $order_no = explode("_",$out_trade_no);

            $orderModel = new IncubatorOrder();
            $orderInfo = $orderModel->where(['order_no'=>$order_no[0],'pay_status'=>0])->find();
            if ($orderInfo){
                $orderInfo->save(['pay_type'=>'wechat','pay_status'=>1,'transaction_id'=>$data['transaction_id'],'pay_time'=>time()]);

                $user = \app\admin\model\User::get($orderInfo['user_id']);
                $user->is_incubator = 1;
                $user->incubator_deposit = $orderInfo['price'];
                $user->incubator_order_id = $orderInfo['id'];
                $user->incubator_num = $orderInfo['num'];
                $user->can_print = 1;
                $user->save();
                /*\app\common\model\User::update([
                    'is_incubator'=>1,
                    'incubator_deposit'=>$orderInfo['price'],
                    'incubator_order_id'=>$orderInfo['id'],
                    'incubator_num'=>$orderInfo['num'],
                ],['id'=>$orderInfo['user_id']]);*/
            }

        } catch (Exception $e) {
            \think\Log::record($e->getMessage());
        }
        echo $pay->success();
    }
}